{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class compareItems():\n",
    "    def __init__(self):\n",
    "        self.oldProductList=[]\n",
    "        self.newProductList=[]\n",
    "        self.insertList=[]\n",
    "        self.deleteList=[]\n",
    "        self.CutPriceList=[]\n",
    "        self.newArrivalList=[]\n",
    "        \n",
    "    def setOldProductList(self,oldProductList):\n",
    "        self.oldProductList=oldProductList\n",
    "        \n",
    "    def setNewProductList(self,newProductList):\n",
    "        self.newProductList=newProductList\n",
    "        \n",
    "    def getInsertList(self):\n",
    "        return self.insertList\n",
    "    \n",
    "    def getDeleteList(self):\n",
    "        return self.deleteList\n",
    "    \n",
    "    def getCutPriceList(self):\n",
    "        return self.CutPriceList\n",
    "    \n",
    "    def getNewArrivalList(self):\n",
    "        return self.newArrivalList\n",
    "        \n",
    "    def compareIfSoldOrCutPrice(self): #用数据库中储存的数据比较新爬取的数据，查看是否存在已售出或降价\n",
    "        i=0\n",
    "        j=0\n",
    "        for i in range(len(self.oldProductList)):\n",
    "            for j in range(len(self.newProductList)):\n",
    "                if self.oldProductList[i].getProductURL()[33:]==self.newProductList[j].getProductURL()[33:]:\n",
    "                    if self.oldProductList[i].getPrice()!=int(self.newProductList[j].getPrice()):\n",
    "                        self.insertList.append(self.newProductList[j])\n",
    "                        self.deleteList.append(self.oldProductList[i])\n",
    "                        if self.oldProductList[i].getPrice()<int(self.newProductList[j].getPrice()):\n",
    "                            self.CutPriceList.append(self.newProductList[j])\n",
    "                    break\n",
    "            if j==len(self.newProductList)-1 and self.oldProductList[i].getProductURL()[33:]!=self.newProductList[j].getProductURL()[33:]:\n",
    "                self.deleteList.append(self.oldProductList[i])\n",
    "        return 1\n",
    "    \n",
    "    def compareIfNew(self): #用新爬取的数据比较数据库中储存的数据，查看是否存在新增商品\n",
    "        i=0\n",
    "        j=0\n",
    "        for i in range(len(self.newProductList)):\n",
    "            for j in range(len(self.oldProductList)):\n",
    "                if self.newProductList[i].getProductURL()[33:]==self.oldProductList[j].getProductURL()[33:]:\n",
    "                        break\n",
    "            if j==len(self.oldProductList)-1 and self.newProductList[i].getProductURL()[33:]!=self.oldProductList[j].getProductURL()[33:]:\n",
    "                self.insertList.append(self.newProductList[i])\n",
    "                self.newArrivalList.append(self.newProductList[i])\n",
    "        if len(self.oldProductList)==0:\n",
    "            self.insertList.extend(self.newProductList)\n",
    "            self.newArrivalList.extend(self.newProductList)\n",
    "        print('本次降价了'+str(len(self.CutPriceList))+'件商品')  \n",
    "        print('本次新增了'+str(len(self.newArrivalList))+'件商品')\n",
    "        print('本次售出了'+str(len(self.deleteList)-len(self.CutPriceList))+'件商品')  \n",
    "        return 1\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
